﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using myDiet;
using System.IO.IsolatedStorage;

namespace delizios
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        // page exit 
        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("Do you really want to exit?", "Message", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
            { 
                e.Cancel = true; 
            }
            else
            {
                if (NavigationService.CanGoBack)
                {
                    while (NavigationService.RemoveBackEntry() != null)
                    {
                        NavigationService.RemoveBackEntry();
                    }
                }
            }
 
        }


//============================================ show/hide Menu Guide =========================================================
        private void showMenu_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            // show menu guide
            showMenu.Content = "Show";
            XElement xml = XElement.Load("menu.xml");           //load xml file for feed 

            //feed and save to data class
            var items = from item in xml.Descendants("food")            //start at "food" element
                        select new data
                        {
                            UserImage = item.Element("picture").Value,      //feed value of "picture" element
                            Text = item.Element("name").Value,              //feed value of "name" element
                            DateTime = item.Element("data").Value           //feed value of "data" element
                        };
            lstTwitter.ItemsSource = items;                 //show data in ListBox
        }

        private void showMenu_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
            // hide menu guide
            showMenu.Content = "Hide";
            lstTwitter.ItemsSource = "";            //set ListBox = null
        }


        // logout click >> to delete userXML.xml file !!
        private void logout_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // delete file !!
                    myIsolatedStorage.DeleteFile("userXML.xml");
                }
            }
            catch { MessageBox.Show("File not found !"); }

            NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
        }
//============================================================================================================================
    }
}